﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.ServiceModel;

namespace DMSClient.Unilever
{
    public partial class Surtidos : Form
    {

        private DMSServices.DMSServicesClient client;
        private DataTable dt;
        DataTable dtSurtidos;
        private string ambient;

        public Surtidos()
        {
            InitializeComponent();
            this.ambient = Properties.Settings.Default.CurrentEnv;
            this.Text = this.ambient + " - Assorted Report";

            WSHttpBinding Binding = new WSHttpBinding();
            Binding.MaxReceivedMessageSize = Int32.MaxValue;
            Binding.MaxBufferPoolSize = Int32.MaxValue;
            Binding.CloseTimeout = TimeSpan.MaxValue;
            Binding.ReceiveTimeout = TimeSpan.MaxValue;
            Binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
            Binding.Security.Mode = SecurityMode.None;

            EndpointAddress address = new EndpointAddress(Properties.Settings.Default.Server);
            client = new DMSServices.DMSServicesClient(Binding, address);            
        }

        private void Surtidos_Load(object sender, EventArgs e)
        {
            uceCountry.DisplayMember = "dsCountry";
            uceCountry.ValueMember = "cdCountry";
            string distributors = client.getCountryList(this.ambient);

            dt = new DataTable("dtCountryList");
            dt.Columns.Add("idCountry");
            dt.Columns.Add("cdCountry");
            dt.Columns.Add("dsCountry");

            DataSet ds = new DataSet();
            ds.Tables.Add(dt);

            System.IO.StringReader xmlSR = new System.IO.StringReader(distributors);
            ds.ReadXml(xmlSR, XmlReadMode.ReadSchema);
            uceCountry.DataSource = dt;
            uceCountry.SelectedIndex = 0;
            xmlSR.Close();
        }


        private void cargarGrid()

        {
            string cdPais = uceCountry.Value.ToString();

            WSHttpBinding binding = new WSHttpBinding();
            binding.CloseTimeout = new TimeSpan(0, 1, 0);
            binding.OpenTimeout = new TimeSpan(0, 1, 0);
            binding.ReceiveTimeout = new TimeSpan(0, 10, 0);
            binding.SendTimeout = new TimeSpan(0, 1, 0);
            binding.MaxReceivedMessageSize = Int32.MaxValue;
            binding.MaxBufferPoolSize = Int32.MaxValue;
            binding.ReaderQuotas.MaxArrayLength = Int32.MaxValue;
            binding.ReaderQuotas.MaxStringContentLength = Int32.MaxValue;
            binding.Security.Mode = SecurityMode.None;



            EndpointAddress address = new EndpointAddress(Properties.Settings.Default.Server);
            DMSServices.DMSServicesClient service = new DMSServices.DMSServicesClient(binding, address);            

            string respuesta = null;
            //--DATOS FATURA
            try
            {
                service.Open();
                respuesta = service.getSurtidosReport(cdPais,this.ambient);
                service.Close();
                respuesta = Compression.Decompress(respuesta);

                dtSurtidos = new DataTable("SurtidosDT");
                dtSurtidos.Columns.Add("dsCountry");
                dtSurtidos.Columns.Add("cdSKU");
                dtSurtidos.Columns.Add("dsProduct");

                DataSet dataSet = new DataSet("SurtidosDS");
                dataSet.Tables.Add(dtSurtidos);
                System.IO.StringReader xmlSR = new System.IO.StringReader(respuesta);
                dataSet.ReadXml(xmlSR, XmlReadMode.ReadSchema);
                xmlSR.Close();
            }
            catch (Exception ex)
            {
                int pascuastas = 0;
            }                            
        }

       
        private void uceCountry_ValueChanged(object sender, EventArgs e)
        {
            ugSurtidos.Text = "";
            ugSurtidos.DataSource = null;
        }

        private void bgWorker_surtidos_DoWork(object sender, DoWorkEventArgs e)
        {
            cargarGrid();
        }

        private void bgWorker_surtidos_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
           // dtSurtidos.Rows.RemoveAt(0);
            ugSurtidos.DataSource = dtSurtidos;
            ugSurtidos.DataBind();
            activarControles(true);
        }

        private void activarControles(bool value)
        {
            loader.Visible = !value;
            ugSurtidos.Enabled = value;
            ultraGroupBox1.Enabled = value;
        }

        private void ultraBTN_Buscar_Click(object sender, EventArgs e)
        {
            activarControles(false);
            bgWorker_surtidos.RunWorkerAsync();
        }

        private void bntExport_Click(object sender, EventArgs e)
        {
            if (ugSurtidos.Rows.Count < 1)
            {
                MessageBox.Show(this, "There is no data to export", "Assorted Report", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            System.Windows.Forms.SaveFileDialog dialogo = new System.Windows.Forms.SaveFileDialog();
            dialogo.DefaultExt = "xls";
            Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter Exporter = new Infragistics.Win.UltraWinGrid.ExcelExport.UltraGridExcelExporter();
            if (dialogo.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Exporter.Export(ugSurtidos, dialogo.FileName);
                if (MessageBox.Show(this, "Do you want to open the file?", "Assorted Report", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    System.Diagnostics.Process.Start(dialogo.FileName);
                }
            }
        }

    }
}
